<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>

    <style>
        
    </style>
</head>


<body>
    
    <script>

        new Promise( (resolve, reject) => {
            setTimeout( () => {
                resolve('接收到异步任务的结果...')
            }, 1200 )
        } )
            .then(
                value => console.log(value)
            )
            .catch(
                reason => console.log(reason)
            )
            .finally(
                () => {
                    console.log('无论promise最后的状态如何, finally中的代码(onFinally回调)都会被执行')
                }
            )






        const p1 = Promise.resolve('p1').then(value => console.log(value))
        const p2 = Promise.reject('p2').then(null, reason => console.log(reason))
        const p3 = Promise.reject('p3').then(undefined, reason => console.log(reason))
        const p4 = Promise.reject('p4').catch(reason => console.log(reason))






        /* 
            Promise.all()方法接收一个数组作为实参
            数组中包含一个或多个Promise
            返回值是个Promise
            
            1. 数组全部Promise都成功时数组all返回值的Promise状态才为成功,
                all返回的Promise的结果就是所有成功Promise的返回值组成的数组
                无论请求的响应顺序是什么,数组的顺序总是对应实参传入时的顺序
            
            2. 数组中只要有一个Promise失败最终all返回的Promise状态就为失败,
                all返回的Promise的结果就是最先失败的Promise的值
        */
       var p5 = Promise.resolve('p5')
       var p6 = new Promise((resolve, reject)=>{
           setTimeout(()=>{
               reject('p6')
            }, 1200)
        })
        var p7 = Promise.resolve('p7')
        var p8 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                reject('p8')
             }, 1100)
         })

        
        Promise.all([p5, p6, p7, p8])
        .then(
            values => console.log('values', values),
            reason => console.log('reason', reason) // 'reason p8'
        )


        
        /* 
            Promise.race()方法接收一个数组作为实参
            数组中包含一个或多个Promise
            返回值是个Promise

            返回的Promise的状态、值由数组中最快响应的Promsie所决定
        */
        var p9 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve('p9')
            }, 1200)
        })
        var p10 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                reject('p10')
            }, 1100)
        })
        var p11 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve('p11')
            }, 1300)
        })

        Promise.race([p9, p10, p11])
        .then(
            value => console.log(value),
            reason => console.log(reason)
        )


        /* 
            Promsise.any()接收一个数组作为实参
            数组中包含一个或多个Promise
            返回值是个Promise

            返回值的Promsie的状态、值由数组中最先成功Promise的那一个Promise决定
            
            当数组中所有的Promise都是失败状态时返回值的Promise的状态会是失败, 值是一个AggregateError对象提示所有Promise都是失败的
        */
        var p12 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                reject('p2')
            }, 1200)
        })
        var p13 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                reject('p13')
            }, 1100)
        })
        var p14 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                reject('p14')
            }, 1000)
        })
        
        Promise.any([p12, p13, p14])
        .then(
            value => console.log(value),
            reason => console.log(reason) // AggregateError: All promises were rejected
        )


        /* 
            Promsise.allSettled()接收一个数组作为实参
            数组中包含一个或多个Promise
            返回值是个Promise

            返回值的Promsie的
                状态一定是成功的
                值是一个数组, 数组中每个对象对应实参给定的Promise的状态和值
        */
        
        var p15 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve('p15')
            }, 1200)
        })
        var p16 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve('p16')
            }, 1100)
        })
        var p17 = new Promise((resolve, reject)=>{
            setTimeout(()=>{
                resolve('p17')
            }, 1000)
        })
        
        Promise.allSettled([p15, p16, p17])
        .then(
            values => console.log('value', values),
            /* 
                [
                 0: {status: "fulfilled", value: "p15"}
                 1: {status: "fulfilled", value: "p16"}
                 2: {status: "fulfilled", value: "p17"}
                ] 
            */
            reason => console.log(reason)
        )

    </script>
</body>

</html>